<template>
  <div class="photo">
    <CardBox title="科普展馆相片"></CardBox>
    <div class="box">
      <div
        class="carousel"
        v-for="(item, index) in photos"
        :key="index"
        v-show="index === currentIndex - 1"
      >
        <img :src="item.img" class="carousel-img" alt="轮播图" />
        <div class="caption">
          {{ item.caption }}
        </div>
        <div class="dots">
          <span
            v-for="idx in photos.length"
            :key="idx - 1"
            :class="['dot', { active: idx === currentIndex }]"
            @click="goTo(idx - 1)"
          ></span>
        </div>
        <img src="@/assets/img/bg-highlight.png" alt="" class="bg-highlight" />
      </div>
    </div>
  </div>
</template>

<script>
import CardBox from "@/components/Global/CardBox.vue";
export default {
  components: {
    CardBox,
  },
  data() {
    return {
      photos: [],
      photo1: [
        {
          img: require("@/assets/img/photo1-1.png"),
          caption: "第一张图片说明",
        },
        {
          img: require("@/assets/img/photo1-2.png"),
          caption: "第二张图片说明",
        },
      ],
      photo2: [
        {
          img: require("@/assets/img/photo2-1.png"),
          caption: "第一张图片说明",
        },
        {
          img: require("@/assets/img/photo2-2.png"),
          caption: "第二张图片说明",
        },
      ],
      photo3: [
        {
          img: require("@/assets/img/photo3-1.png"),
          caption: "第一张图片说明",
        },
        {
          img: require("@/assets/img/photo3-2.png"),
          caption: "第二张图片说明",
        },
      ],
      photo4: [
        {
          img: require("@/assets/img/photo4-1.png"),
          caption: "第一张图片说明",
        },
        {
          img: require("@/assets/img/photo4-2.png"),
          caption: "第二张图片说明",
        },
      ],
      photo5: [
        {
          img: require("@/assets/img/photo5-1.png"),
          caption: "第一张图片说明",
        },
      ],
      photo6: [
        {
          img: require("@/assets/img/photo6-1.png"),
          caption: "第一张图片说明",
        },
      ],
      scienceId: this.$route.query.scienceId,
      currentIndex: 1,
    };
  },
  mounted() {
    // 初始化时根据 scienceId 设置对应的照片
    this.updatePhotos();
  },
  methods: {
    goTo(index) {
      this.currentIndex = index + 1;
    },
    updatePhotos() {
      if (this.scienceId === 'xinjian01' || !this.scienceId) {
        this.photos = this.photo1;
      } else if (this.scienceId === 'haipai04') {
        this.photos = this.photo2;
      } else if (this.scienceId === 'guonei02') {
        this.photos = this.photo3;
      } else if (this.scienceId === 'xinjian02') {
        this.photos = this.photo4;
      } else if (this.scienceId === 'wenshi01') {
        this.photos = this.photo5;
      } else if (this.scienceId === 'haipai02') {
        this.photos = this.photo6;
      }
      // 重置轮播索引
      this.currentIndex = 1;
    }
  },
  watch: {
    "$route.query.scienceId": {
      handler(newScienceId) {
        this.scienceId = newScienceId;
        this.updatePhotos();
      },
      immediate: true,
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  background: url("@/assets/img/container-bg.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: center center;
  padding: 18px;
  height: 403px;
  .carousel {
    width: 100%;
    height: 100%;
    position: relative;
    .carousel-img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
    .caption {
      position: absolute;
      left: 0;
      bottom: 0px;
      color: #fff;
      background: rgba(0,0,0,0.3);
      width: 100%;
      font-size: 14px;
      padding: 4px 12px;
    }
    .dots {
      position: absolute;
      right: 10px;
      bottom: 10px;
      display: flex;
      z-index: 2;
      gap: 6px;
      .dot {
        width: 20px;
        height: 10px;
        background: #fff;
        opacity: 0.5;
        cursor: pointer;
        &.active {
          background: #07ffe5;
          opacity: 1;
        }
      }
    }
    .bg-highlight {
      position: absolute;
      right: -3px;
      bottom: -16px;
    }
  }
}
</style>